﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Castle.ActiveRecord;

namespace TVN.ShopMan.ActiveImpl.Repositories
{
    using TVN.Common.Interfaces;

    public abstract class RepositoryBase<T> : IRepository<T>
        where T : ActiveRecordBase<T>
    {
        public RepositoryBase()
        {
            AppRunner.Initialize();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <param name="objT"></param>
        public virtual void Insert(T objT)
        {
            objT.Save();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <param name="objT"></param>
        public virtual void Update(T objT)
        {
            objT.Update();
        }

        /// <summary>
        /// Delete object
        /// </summary>
        /// <param name="objT"></param>
        public virtual void Delete(T objT)
        {
            objT.Delete();
        }

        /// <summary>
        /// Get object by its id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public abstract T GetById(object id);

        /// <summary>
        /// Get all objects
        /// </summary>
        public virtual IList<T> FindAll()
        {
            return this.FindBy(null);
        }

        /// <summary>
        /// Get by dictionary of condition
        /// </summary>
        /// <param name="propertyNameValues">Contains properties and values</param>
        public abstract IList<T> FindBy(IDictionary propertyNameValues);
    }
}
